#-*- coding: UTF-8 -*-

from futu import *
import pandas as pd
import time
import logging


class quoteapi():

    def __init__(self):
        self.tablename = "hqtable"
        self._host = '127.0.0.1'
        self._port = 11111
        self._password_md5 ='d6d1d2b50655a964810ba5592c9200a5'
    
    def init_context(self):
        SysConfig.set_all_thread_daemon(True) #在主线程退出后，如果其余线程都是 daemon 线程，则进程退出
        self.context = OpenQuoteContext(host=self._host, port=self._port, is_encrypt=None)
      
    def init_logconfig(self, logpathfile = '../logs/futu_trade.log'):
        logging.basicConfig(filename=logpathfile,format='[%(asctime)s-%(filename)s-%(levelname)s:%(message)s]', level = logging.DEBUG,filemode='a',datefmt='%Y-%m-%d%I:%M:%S %p')
        
    def get_global_state(self):
        return self.context.get_global_state()
    
    def get_option_chain(self, code, index_option_type=IndexOptionType.NORMAL, start=None, end=None, option_type=OptionType.ALL, option_cond_type=OptionCondType.ALL, data_filter=None):
        return  self.context.get_option_chain(code, index_option_type, start, end, option_type, option_cond_type, data_filter)
    
    def subscribe(self, code_list, subtype_list, is_first_push=True, subscribe_push=True, is_detailed_orderbook=False, extended_time=False):
        return self.context.subscribe(code_list, subtype_list, is_first_push, subscribe_push, is_detailed_orderbook, extended_time)
    
     #  ------------------基本信息----------------------------------------------------
    def get_stock_basicinfo(self, market, stock_type=SecurityType.STOCK, code_list=None):
        return self.context.get_stock_basicinfo(market, stock_type, code_list)
    
    # 获取自选股列表
    def get_user_security_group(self, group_type = UserSecurityGroupType.ALL):
        return self.context.get_user_security_group(group_type)
    
    def get_user_security(self, group_name):
        return self.context.get_user_security(group_name)
    
    # 修改自选股  ADD  DEL MOVE_OUT
    def modify_user_security(self, group_name, op, code_list):
        return self.context.modify_user_security(group_name, op, code_list)
        
    #SecurityReferenceType WARRANT  FUTURE UNKNOW
    def get_referencestock_list(self, code, reference_type):
        return self.context.get_referencestock_list(code, reference_type)
    
    #  -------------------------------行情信息---------------------------------------
    def request_history_kline(self, code, start=None, end=None, ktype=KLType.K_DAY, autype=AuType.QFQ, fields=[KL_FIELD.ALL], max_count=1000, page_req_key=None, extended_time=False):
        return self.context.request_history_kline(code, start, end, ktype, autype, fields, max_count, page_req_key, extended_time)
        
    def get_market_snapshot(self, code_list):
        return self.context.get_market_snapshot(code_list)
    
    def closecontext(self):
        self.context.close()
        
    
        
